package cn.chenmanman.system.utils;

import cn.chenmanman.model.system.SysMenu;

import java.util.ArrayList;
import java.util.List;

public class MenuHelper {

    public static List<SysMenu> buildTrees(List<SysMenu> menus) {
        List<SysMenu> sysMenuList = new ArrayList<>();

        for (SysMenu menu:menus) {
            if(menu.getParentId() == 0) {
                sysMenuList.add(findChildren(menu, menus));
            }
        }
        return sysMenuList;
    }

    public static SysMenu findChildren(SysMenu menu, List<SysMenu> menus) {
        menu.setChildren(new ArrayList<>());
        for (SysMenu sysMenu: menus) {
            if (Long.parseLong(menu.getId()) == sysMenu.getParentId()) {
                if(menu.getChildren() == null) menu.setChildren(new ArrayList<>());
                menu.getChildren().add(findChildren(sysMenu, menus));
            }
        }
        return menu;
    }


}
